﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Text.RegularExpressions;
using Total_Mayhem.Common;

namespace Total_Mayhem.Networking
{
    /// <summary>
    /// Interaction logic for NetworkConfig.xaml
    /// </summary>
    public partial class NetworkEventConfig : MayhemWpf.UserControls.WpfConfiguration
    {

        public NetworkEventConfig(string ComputerName, int IntervalinSeconds, string Title = "Computer Joins Network", string Verb = "joins")
        {
            InitializeComponent();
            this.ComputerName = ComputerName;
            this.IntervalinSeconds = IntervalinSeconds;
            //this.WaitinMilliSeconds = WaitinMilliSeconds;
            this.strTitle = Title;
            runVerb.Text = Verb;
        }

        public override void OnLoad()
        {
            //Set wpf element text here
            textBoxName.Text = ComputerName;
            if (string.IsNullOrEmpty(textBoxName.Text) && Environment.MachineName == "GB7") { textBoxName.Text = "android_bc685d7e8081788a"; } //Default to my phone ID if on my machine (simplify testing)
            var saved = cmbInterval.Items.Cast<ComboBoxItem>().FirstOrDefault(i => i.Tag.ToString() == IntervalinSeconds.ToString());
            if (saved != null)
            {
                saved.IsSelected = true;
            }
            //var saved2 = cmbWait.Items.Cast<ComboBoxItem>().FirstOrDefault(i => i.Tag.ToString() == WaitinMilliSeconds.ToString());
            //if (saved2 != null)
            //{
            //    saved2.IsSelected = true;
            //}
        }

        private string strTitle;
        public override string Title
        {
            get
            {
                return strTitle;
            }
        }

        public string ComputerName { get; private set; }
        public int IntervalinSeconds { get; private set; }
        //public int WaitinMilliSeconds { get; private set; }


        private void Name_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckValidity();
            ComputerName = textBoxName.Text;
        }

        private void CheckValidity()
        {
            CanSave = true;
            if (Network.RegexIP.IsMatch(textBoxName.Text) || Network.RegexName.IsMatch(textBoxName.Text))
            {
                btnTest.IsEnabled = true;
            }
            else
            {
                textInvalid.Text = "Invalid Computer Name or IP Address";
                CanSave = false;
                btnTest.IsEnabled = true;
            }

            textInvalid.Visibility = CanSave ? Visibility.Collapsed : Visibility.Visible;
        }

        private void CheckSubdirs_Checked(object sender, RoutedEventArgs e)
        {

        }

        private void btnTest_Click(object sender, RoutedEventArgs e)
        {
            /*try
            {
                System.Net.NetworkInformation.Ping p = new System.Net.NetworkInformation.Ping();
                var pr = p.Send(textBoxName.Text);
                if (pr.Status != System.Net.NetworkInformation.IPStatus.Success)
                {
                    textInvalid.Text = "Error: " + pr.Status;
                }
                else
                {
                    textInvalid.Text = string.Format("Success! Response Time: {0:#,000}ms",pr.RoundtripTime);
                }
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                {
                    textInvalid.Text = "Exception: " + ex.InnerException.Message;
                }
                else
                {
                    textInvalid.Text = "Exception: " + ex.Message;
                }
            }
            */
            textInvalid.Visibility = System.Windows.Visibility.Hidden;
            //Delay so that text box has time to clear and then be updated
            Threadding.QueueBackroundTaskwithDelay(TimeSpan.FromMilliseconds(500), () =>
            {
                string strResult = "";
                bool bResult = Network.SendPingRequest(ComputerName, out strResult);
                textInvalid.Dispatcher.Invoke(new Action<string>((text) =>
                {
                    textInvalid.Text = text;
                    textInvalid.Visibility = System.Windows.Visibility.Visible;
                }),strResult);
            });
        }

        private void cmbInterval_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cmbInterval.SelectedItem != null)
            {
                ComboBoxItem i = (cmbInterval.SelectedItem as ComboBoxItem);
                if (i != null && !string.IsNullOrEmpty((string)i.Tag))
                {
                    IntervalinSeconds = int.Parse(i.Tag.ToString());
                }
            }
        }

        private void cmbWait_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cmbWait.SelectedItem != null)
            {
                ComboBoxItem i = (cmbWait.SelectedItem as ComboBoxItem);
                if (i != null && !string.IsNullOrEmpty((string)i.Tag))
                {
                    //WaitinMilliSeconds = int.Parse(i.Tag.ToString());
                }
            }
        }
    }
}
